package com.kakarote.core.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ClassUtil;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.NacosServiceManager;
import com.alibaba.cloud.nacos.discovery.NacosWatch;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

import java.util.Date;

/**
 * nacos客户端配置
 *
 * @author zhangzhiwei
 */
@AutoConfigureBefore({com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration.class})
public class NacosClientConfiguration {

    @Bean
    @ConditionalOnProperty(value = {"spring.cloud.nacos.discovery.watch.enabled"}, matchIfMissing = true)
    @ConditionalOnMissingBean
    public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager, NacosDiscoveryProperties nacosDiscoveryProperties) {
        nacosDiscoveryProperties.getMetadata().put("projectPath", ClassUtil.getClassPath());
        nacosDiscoveryProperties.getMetadata().put("startTime", DateUtil.formatDateTime(new Date()));
        return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties);
    }

}